package sdu.icat.pj.sys.web;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sdu.icat.pj.base.ResponseMap;
import sdu.icat.pj.service.SysArticleService;
import sdu.icat.sys.smodel.SysArticle;

/**
 * <br>
 * 文章
 * </br>
 * @author ljh_2015
 *
 */
@RestController
@Api(value="article manager",tags={"article manager"})
@RequestMapping("/sys_article")
public class SysArticleController {

    @Autowired private SysArticleService sysArticleService;

    @PostMapping("/publish")
    @ApiOperation(value="发表文章",notes="发表管理信息文章，例如使用说明，官方文档等")
    @RequiresPermissions("sys.base.article")
    public ResponseMap publish(@RequestBody SysArticle sysArticle) {
	ResponseMap result = new ResponseMap();
	SysArticle article = sysArticleService.save(sysArticle);
	if(article!=null) {
	    result.setStatus(200);
	    result.putMsg("成功发表文章");
	    result.setData(sysArticle);
	} else {
	    result.setStatus(-1);
	    result.putMsg("发表文章失败");
	}
	return result;
    }

    @DeleteMapping()
    @ApiOperation(value="删除文章",notes="支持批量删除文章")
    @RequiresPermissions("sys.base.article")
    public ResponseMap delete(long[] ids) {
	ResponseMap result = new ResponseMap();
	if(sysArticleService.delete(ids)) {
	    result.setStatus(200);
	    result.putMsg("成功删除 "+ids.length+" 篇文章");
	} else {
	    result.setStatus(-1);
	    result.putMsg("sorry!删除失败!");
	}
	return result;
    }
    
    @PutMapping("query")
    @ApiOperation(value="查询文章",notes="按条件查询文章")
    @RequiresPermissions("sys.base.article")
    public ResponseMap query(@RequestBody Map<String,Object> params) {
	ResponseMap result = new ResponseMap();
	Page<SysArticle> articles = sysArticleService.listParams(params);
	result.setStatus(200);
	result.putMsg("查询到"+ articles.getSize() + "篇文章");
	result.setData(articles);
	return result;
    }
    
    @PutMapping("allQuery")
    @ApiOperation(value="获取所有文章",notes="获取文章")
    @RequiresPermissions("sys.base.article")
    public ResponseMap list(int page,int size) {
	ResponseMap result = new ResponseMap();
	Page<SysArticle> articles = sysArticleService.listAll(page, size);
	result.setStatus(200);
	result.putMsg("查询到"+ articles.getTotalElements() + "篇文章");
	result.setData(articles);
	return result;
    }
 }
